1 using System;
2 using
System.Collections.Generic;
3 using
System.IO;
4 using
UnityEditor;
5 using
UnityEngine;
6
7 [Serializable]

8 public
class SceneSetting
9 {
10     
public string sceneName;
11     
public int minViewId;
12 }

13
14 public
class PunSceneSettings : ScriptableObject
15 {
16     [SerializeField]
public List<SceneSetting> MinViewIdPerScene = new List<SceneSetting>();
17
18     
private const string SceneSettingsFileName = "PunSceneSettingsFile.asset";
19
20     
// we use the path to PunSceneSettings.cs as path to create a scene settings file
21     
private static string punSceneSettingsCsPath;
22     
public static string PunSceneSettingsCsPath
23     {
24         
get
25         {
26             
if (!string.IsNullOrEmpty(punSceneSettingsCsPath))
27             {
28                 
return punSceneSettingsCsPath;
29             }
30
31             
// Unity 4.3.4 does not yet have AssetDatabase.FindAssets(). Would be easier.
32             
var result = Directory.GetFiles(Application.dataPath, "PunSceneSettings.cs", SearchOption.AllDirectories);
33             
if (result.Length >= 1)
34             {
35                 punSceneSettingsCsPath = Path.GetDirectoryName(result[
0]);
36                 punSceneSettingsCsPath = punSceneSettingsCsPath.Replace(
'\\', '/');
37                 punSceneSettingsCsPath = punSceneSettingsCsPath.Replace(Application.dataPath,
"Assets");
38
39                 
// AssetDatabase paths have to use '/' and are relative to the project's folder. Always.
40                 punSceneSettingsCsPath = punSceneSettingsCsPath +
"/" + SceneSettingsFileName;
41             }
42
43             
return punSceneSettingsCsPath;
44         }
45     }
46
47
48     
private static PunSceneSettings instanceField;
49     
public static PunSceneSettings Instance
50     {
51         
get
52         {
53             
if (instanceField != null)
54             {
55                 
return instanceField;
56             }
57
58             instanceField = (PunSceneSettings)AssetDatabase.LoadAssetAtPath(PunSceneSettingsCsPath,
typeof(PunSceneSettings));
59             
if (instanceField == null)
60             {
61                 instanceField = ScriptableObject.CreateInstance<PunSceneSettings>();
62                 AssetDatabase.CreateAsset(instanceField, PunSceneSettingsCsPath);
63             }
64
65             
return instanceField;
66         }
67     }
68
69
70     
public static int MinViewIdForScene(string scene)
71     {
72         
if (string.IsNullOrEmpty(scene))
73         {
74             
return 0;
75         }
76
77         PunSceneSettings pss = Instance;
78         
if (pss == null)
79         {
80             Debug.LogError(
"pss cant be null");
81             
return 0;
82         }
83
84         
foreach (SceneSetting setting in pss.MinViewIdPerScene)
85         {
86             
if (setting.sceneName.Equals(scene))
87             {
88                 
return setting.minViewId;
89             }
90         }
91         
return 0;
92     }
93 }


we use the path to PunSceneSettings.cs as path to create a scene settings file

Unity 4.3.4 does not yet have AssetDatabase.FindAssets(). Would be easier.

AssetDatabase paths have to use '' and are relative to the project's folder. Always.




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.582 lượt xem

Gõ tìm kiếm nhanh...